package com.bootdo.clouddo.service.book.impl;

import com.bootdo.clouddo.dao.book.BookDetailMapper;
import com.bootdo.clouddo.domain.book.BookDetail;
import com.bootdo.clouddo.redis.CommonRedisService;
import com.bootdo.clouddo.redis.RedisEnum;
import com.bootdo.clouddo.service.book.IBookDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookDetailServiceImpl implements IBookDetailService {

    @Autowired
    private BookDetailMapper dao;

    @Autowired
    private CommonRedisService redisService;

    @Override
    public int save(BookDetail record) {
        if(dao.selectByPrimaryKey(record.getId()) != null) {
            return dao.updateByPrimaryKeySelective(record);
        }
        return dao.insertSelective(record);
    }

    @Override
    public BookDetail findById(Long id) {
        return dao.selectByPrimaryKey(id);
    }

    @Override
    public int updateSiteId(Long bookId, Integer siteId) {
        BookDetail bookDetail = new BookDetail();
        bookDetail.setId(bookId);
        bookDetail.setSiteId(siteId);
        int result = dao.updateByPrimaryKeySelective(bookDetail);
        if(result > 0) {
            clearCache(bookId);
        }
        return result;
    }

    void clearCache(Long id) {
        redisService.remove(RedisEnum.BOOK_DETAIL.getKey() + id);
    }
}
